package com.link2room.ironhide.controller.api;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.util.StringUtil;
import com.link2room.ironhide.dto.param.api.HotelsApiParamDto;
import com.link2room.ironhide.dto.result.api.HotelBaseInfoApiDto;
import com.link2room.ironhide.dto.result.manager.L2RServiceDto;
import com.link2room.ironhide.facade.IHotelApiFacadeService;
import com.link2room.ironhide.facade.IHotelInfoFacadeService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sss on 2016/9/18.
 */
@Api(tags= "酒店相关")
@RestController
@RequestMapping("/api/Hotels")
public class HotelsApiController {
    @Resource
    private IHotelApiFacadeService hotelApiFacadeService;
    @Resource
    private IHotelInfoFacadeService hotelInfoFacadeService;

    @RequestMapping(method = RequestMethod.GET)
    public BaseResultDto hotels(
            @RequestParam(value = "lhotelCode" ,required = false) String lhotelCode,
            @RequestParam(value = "province" ,required = false) Long province,
            @RequestParam(value = "city" ,required = false) Long city,
            @RequestParam(value = "positions" ,required = false) String positions,
            @RequestParam(value = "brands" ,required = false) String brands,
            @RequestParam(value = "checkInDatetime" ,required = false) String checkInDatetime,
            @RequestParam(value = "checkOutDatetime" ,required = false) String checkOutDatetime,
            @RequestParam(value = "longitude" ,required = false) Double longitude,
            @RequestParam(value = "latitude" ,required = false) Double latitude,
            @RequestParam (value = "key" ,required = false) String key,
            @RequestParam(value = "sortBy" ,required = false) String sortBy,
            @RequestParam(value = "order" ,required = false) String order
    ) throws Exception {
        HotelsApiParamDto hotelsApiParamDto=new HotelsApiParamDto();
        hotelsApiParamDto.setLhotelCode(lhotelCode);
        hotelsApiParamDto.setCity(city);
        hotelsApiParamDto.setProvince(province);
        hotelsApiParamDto.setOrder(order);
        hotelsApiParamDto.setSortBy(sortBy);
        hotelsApiParamDto.setKey(key);
        if(StringUtil.isNotBlank(brands)){
            List<String> brandList=new ArrayList();
            String[] brandArr = brands.split(",");
            for(String brand:brandArr){
                brandList.add(brand);
            }
            hotelsApiParamDto.setBrands(brandList);
        }
        if(StringUtil.isNotBlank(positions)){
            List<String> positionList=new ArrayList();
            String[] positionArr = positions.split(",");
            for(String position:positionArr){
                positionList.add(position);
            }
            hotelsApiParamDto.setPositions(positionList);
        }
        hotelsApiParamDto.setCheckInDatetime(checkInDatetime);
        hotelsApiParamDto.setCheckOutDatetime(checkOutDatetime);
        hotelsApiParamDto.setLongitude(longitude);
        hotelsApiParamDto.setLatitude(latitude);
        QueryResult<HotelBaseInfoApiDto> hotelBaseInfoResult = hotelApiFacadeService.getHotels(hotelsApiParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(hotelBaseInfoResult);
        return l2rRs;
    }

    @RequestMapping(value = "/HotelServices",method= RequestMethod.GET)
    public BaseResultDto HotelBaseInfoList(
            @RequestParam(value = "lhotelCode" ,required = true) String lhotelCode
    ) throws Exception {
        BaseParamDto baseParamDto=new BaseParamDto();
        baseParamDto.setLhotelCode(lhotelCode);
        List<L2RServiceDto> l2RHotelServices = hotelInfoFacadeService.getL2RHotelServices(baseParamDto);
        BaseResultDto l2rRs=new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RHotelServices);
        return l2rRs;
    }
}
